function [options,data] = checkoptions (options,data,T,cv)


if ~isfield(options,'K'), error('K was not specified'); end
if ~isfield(options,'order'), error('order was not specified'); end
if ~isfield(options,'Fs'), options.Fs = 1; end
if ~isfield(options,'covtype'), options.covtype = 'full'; end
if ~isfield(options,'zeromean'), 
    if options.order>0, options.zeromean = 1; 
    else options.zeromean = 0;
    end
end
if ~isfield(options,'cyc'), options.cyc = 1000; end
if ~isfield(options,'tol'), options.tol = 1e-5; end
if ~isfield(options,'meancycstop'), options.meancycstop = 1; end
if ~isfield(options,'cycstogoafterevent'), options.cycstogoafterevent = 20; end
if ~isfield(options,'initcyc'), options.initcyc = 100; end
if ~isfield(options,'initrep'), options.initrep = 5; end
if ~isfield(options,'inittype'), options.inittype = 'EM'; end
if ~isfield(options,'Gamma'), options.Gamma = []; end
if ~isfield(options,'timelag'), options.timelag = 1; end
if ~isfield(options,'exptimelag'), options.exptimelag = 1; end
if ~isfield(options,'orderoffset'), options.orderoffset = 0; end
if ~isfield(options,'symmetricprior'), options.symmetricprior = 1; end
if ~isfield(options,'whitening'), options.whitening = 0; end
if ~isfield(options,'embeddedlags'), options.embeddedlags = 0; end
if ~isfield(options,'repetitions'), options.repetitions = 1; end
if ~isfield(options,'updateGamma'), options.updateGamma = 1; end
if ~isfield(options,'keepS_W'), options.keepS_W = 1; end
if ~isfield(options,'verbose'), options.verbose = 1; end
if ~isfield(options,'checkpt_fname'), options.checkpt_fname=''; end

if ~strcmp(options.inittype,'random') && options.initrep == 0,
    options.inittype = 'random';
    warning('Non random init was set, but initrep==0')
end

if isempty(options.checkpt_fname) && length(options.cyc)>1,
    warning('cyc was a specified as a vector but checkpt_fname is empty')
    options.cyc = options.cyc(end);
end
    
if ~isempty(options.checkpt_fname) && length(options.cyc)==1,
    warning('checkpt_fname has no effect if cyc is not specified as a vector')
    options.checkpt_fname='';
end

if (options.order>0) && (options.order <= options.orderoffset)
    error('order has to be either zero or higher than orderoffset')
end

if (options.order>0 && options.timelag<1 && options.exptimelag<=1)
    error('if order>0 then you should specify either timelag>=1 or exptimelag>=1')
end   

if ~isstruct(data), data = struct('X',data); end
if ~isfield(data,'C'), 
    if options.K>1, data.C = NaN(size(data.X,1),options.K); 
    else data.C = ones(size(data.X,1),1); 
    end
end
if ~isfield(options,'S'), options.S=ones(size(data.X,2),size(data.X,2)); end
if ~issymmetric(options.S) && options.symmetricprior==0,
   error('In order to use a symmetric prior, you need S to be symmetric as well') 
end
if (strcmp(options.covtype,'full') || strcmp(options.covtype,'uniquefull')) &&  ~all(options.S(:)==1),
    error('if S is not all set to 1, then covtype must be diag or uniquediag')
end

if options.K~=size(data.C,2), error('Matrix data.C should have K columns'); end
if options.K>1 && options.updateGamma == 0 && isempty(options.Gamma), 
    warning('Gamma is unspecified, so updateGamma was set to 1');  options.updateGamma = 1; 
end
if options.updateGamma == 1 && options.K == 1,
    warning('Since K is one, updateGamma was set to 0');  options.updateGamma = 0; 
end
if options.updateGamma == 0 && options.repetitions>1,
    error('If Gamma is not going to be updated, repetitions>1 is unnecessary')
end

if ~isempty(options.Gamma)
    [~,order] = formorders(options.order,options.orderoffset,options.timelag,options.exptimelag);
    if (size(options.Gamma,1) ~= (sum(T) - order*length(T))) || (size(options.Gamma,2) ~= options.K),
        error('The supplied Gamma has not the right dimensions')
    end
end

if cv==1
    if ~isfield(options,'cvfolds'), options.cvfolds = length(T); end
    if ~isfield(options,'cvrep'), options.cvrep = 1; end
    if ~isfield(options,'cvmode'), options.cvmode = 1; end
    if ~isfield(options,'cvverbose'), options.cvverbose = 0; end
    if length(options.cvfolds)>1 && length(options.cvfolds)~=length(T), error('Incorrect assigment of trials to folds'); end
    if length(options.cvfolds)>1 && ~isempty(options.Gamma), error('Set options.Gamma=[] for cross-validating'); end
    if length(options.cvfolds)==1 && options.cvfolds==0, error('Set options.cvfolds to a positive integer'); end
    if options.K==1 && isfield(options,'cvrep')>1, warning('If K==1, cvrep>1 has no point; cvrep is set to 1 \n'); end
    if ~isempty(options.checkpt_fname), warning('Intermediate files are not saved for CV \n'); options.checkpt_fname = ''; end
else
    if ~isempty(options.checkpt_fname) && options.repetitions>1, warning('Intermediate files are not saved repetitions>1 \n'); options.checkpt_fname = ''; end 
end

end

function test = issymmetric(A)
B = A';
test = all(A(:)==B(:)); 
end



